﻿using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigPrescriptionTypesCtrlViewModel : BaseViewModel<ConfigPrescriptionTypesCtrlViewModel>
    {
        #region Variables

        private string _seachValue = "";

        private ObservableCollection<ConfigPrescriptionTypesModel> _disConfigPrescriptionTypesModel = new ObservableCollection<ConfigPrescriptionTypesModel>();
        #endregion

        #region Constructor

        public ConfigPrescriptionTypesCtrlViewModel()
        {
        }

        #endregion

        #region Properties
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }
            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisConfigPrescriptionTypesModel");

            }
        }
        public ObservableCollection<ConfigPrescriptionTypesModel> DisConfigPrescriptionTypesModel
        {
            get
            {
                _disConfigPrescriptionTypesModel.Clear();
                List<ConfigPrescriptionTypesModel> lst = DataManager.Instance.ConfigPrescriptionTypesModelList.ToList().Where(O => O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _disConfigPrescriptionTypesModel.Add(item);
                }
                return _disConfigPrescriptionTypesModel;
            }

            set
            {
                _disConfigPrescriptionTypesModel = value;
                RaisePropertyChanged("DisConfigPrescriptionTypesModel");
            }
        }

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigPrescriptionTypesModelService.Instance.Update(sender as ConfigPrescriptionTypesModel);
        }
        private void AddConfigPrescriptionTypesModel(ConfigPrescriptionTypesModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigPrescriptionTypesModelService.Instance.Update(obj);
                ConfigPrescriptionTypesDataGridViewModel.Instance.ConfigPrescriptionTypesModel.Add(obj);
            }
            else ConfigPrescriptionTypesModelService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigPrescriptionTypesModel(ConfigPrescriptionTypesModel configPrescriptionTypesModel)
        {
            if (configPrescriptionTypesModel != null)
            {
                ConfigPrescriptionTypesDataGridViewModel.Instance.ConfigPrescriptionTypesModel.Remove(configPrescriptionTypesModel);
                RaisePropertyChanged("DisConfigPrescriptionTypesModel");
                ConfigPrescriptionTypesModelService.Instance.DeleteById(configPrescriptionTypesModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigPrescriptionTypesModel model = new ConfigPrescriptionTypesModel();
            DisConfigPrescriptionTypesModel.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigPrescriptionTypesModel(sender as ConfigPrescriptionTypesModel);
        }
        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
